{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cb9e3f4a",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[[ 1.16307855e+00  4.83804613e-01  2.99563468e-01  1.53025463e-01\n",
      "  -1.16881478e+00  1.55807102e+00 -5.45944571e-01 -2.35562968e+00\n",
      "   5.41440248e-01  2.67850637e+00  1.25463438e+00 -5.48774064e-01\n",
      "  -6.81064367e-01 -1.35315597e-01  3.77231330e-01  4.10164565e-01\n",
      "   5.71268201e-01 -2.75796270e+00  1.07628000e+00 -6.14132524e-01\n",
      "   1.83076489e+00 -1.14680648e+00  5.38379401e-02 -2.50748062e+00\n",
      "  -5.91649890e-01  8.58604968e-01 -2.27941826e-01  2.01314762e-01\n",
      "   3.50054711e-01  5.36052108e-01  1.51944435e+00  1.90408790e+00\n",
      "  -1.57344317e+00 -1.40078664e-01  2.96700746e-01  1.31119514e+00\n",
      "   5.03590405e-01 -1.18944502e+00 -5.50213754e-01 -1.59187567e+00\n",
      "  -1.10819471e+00  7.87202045e-02 -9.18563426e-01 -7.45714366e-01\n",
      "  -1.20808625e+00  1.81402123e+00 -1.52274299e+00 -2.51524496e+00\n",
      "  -1.35493255e+00 -9.57484007e-01]\n",
      " [-7.24855661e-01  1.11196363e+00 -4.78272617e-01 -1.17394924e+00\n",
      "  -7.92184532e-01  9.32104170e-01 -3.15577412e+00  2.19352961e+00\n",
      "  -5.38592100e-01 -8.61133277e-01 -1.88151908e+00 -7.20570803e-01\n",
      "  -3.56015712e-01 -1.59639835e-01  1.56065613e-01 -5.68039834e-01\n",
      "  -8.06288600e-01 -1.20004404e+00 -1.18775177e+00 -1.53466478e-01\n",
      "   1.33857489e-01 -7.53186285e-01  1.96709669e+00 -1.39981210e+00\n",
      "   4.39099818e-01 -4.62191343e-01  2.53165752e-01 -2.30282402e+00\n",
      "   1.68812025e+00 -6.52319789e-01  8.90391231e-01 -2.76332527e-01\n",
      "   1.94175375e+00  7.87935376e-01 -8.55672538e-01 -8.23990107e-01\n",
      "   5.18701792e-01  1.53645948e-01  1.01133049e+00 -4.26477164e-01\n",
      "   6.85032606e-01  1.17471194e+00  1.36774385e+00  1.03841746e+00\n",
      "  -1.73056149e+00  6.77059591e-01 -2.50281477e+00 -2.85893530e-01\n",
      "  -3.61471176e-01 -1.09106123e+00]\n",
      " [ 2.46068761e-01 -1.05367899e+00 -2.64701337e-01 -6.32179856e-01\n",
      "   1.24464536e+00  5.74133039e-01  7.54473329e-01 -9.46335614e-01\n",
      "  -1.11048736e-01  1.94201016e+00 -2.33026505e+00  4.77373824e-02\n",
      "   4.00933444e-01  7.44052157e-02  2.16521457e-01  9.10973132e-01\n",
      "  -1.10461283e+00  1.19392550e+00 -2.30875993e+00 -1.00929570e+00\n",
      "   6.21007442e-01 -1.89298785e+00  1.30923641e+00  3.80760849e-01\n",
      "   7.06950545e-01  1.25357759e+00  1.27431488e+00 -7.03455985e-01\n",
      "  -2.64848113e-01  7.12087005e-02 -2.76538467e+00 -7.84274518e-01\n",
      "  -4.50672954e-01 -9.66654003e-01  2.85537690e-01 -1.90459502e+00\n",
      "  -1.37131155e+00  4.41008538e-01  3.13298345e-01 -1.11990702e+00\n",
      "   4.07622367e-01 -8.40986371e-01  8.23291898e-01  1.15115142e+00\n",
      "  -1.61028892e-01  3.39721084e-01  4.90530059e-02 -3.27121049e-01\n",
      "  -1.83054224e-01 -1.08309293e+00]\n",
      " [ 1.99348879e+00 -6.97977841e-01 -3.71074766e-01 -1.42194831e+00\n",
      "  -4.63111639e-01 -5.38398564e-01 -1.42209733e+00  1.25869644e+00\n",
      "  -8.41612443e-02  6.79387078e-02 -1.51051188e+00  2.06350893e-01\n",
      "  -2.21476436e-01  4.53313321e-01  5.41462958e-01  4.45705045e-05\n",
      "  -1.10490942e+00  4.45870429e-01 -3.07461709e-01 -8.48473251e-01\n",
      "   1.52007663e+00  2.75539398e-01  1.87993959e-01 -4.57841575e-01\n",
      "   1.18770552e+00 -4.63626921e-01 -1.55224621e+00  4.92416203e-01\n",
      "   9.43366349e-01  7.78663933e-01  1.58649647e+00 -1.70738208e+00\n",
      "   2.73098230e-01 -6.89480722e-01 -2.77867079e+00  1.06651075e-02\n",
      "  -1.91101575e+00 -1.58608758e+00 -1.57858002e+00 -7.59264946e-01\n",
      "   6.27769828e-01 -1.35236442e+00  8.96679163e-02  4.97167826e-01\n",
      "  -3.67974937e-01  4.32600588e-01  9.43522215e-01 -7.69571841e-01\n",
      "  -5.51053226e-01 -1.94588554e+00]\n",
      " [ 6.17972195e-01 -1.32891572e+00  2.03959727e+00  1.75403148e-01\n",
      "   5.31700663e-02 -1.49965310e+00 -2.15514755e+00 -1.09863794e+00\n",
      "   1.69189322e+00 -4.47196126e-01 -1.20248353e+00  2.74916589e-01\n",
      "   5.38991928e-01 -8.62276196e-01 -2.24485517e-01 -7.69855499e-01\n",
      "   1.18502223e+00  1.09936559e+00 -1.90221691e+00 -8.34691405e-01\n",
      "   6.17363513e-01  4.72076565e-01 -4.62640598e-02 -1.06723881e+00\n",
      "  -2.02730441e+00 -2.06738144e-01 -1.48032129e+00 -6.51789486e-01\n",
      "  -5.67899346e-01  8.82521987e-01  2.36115828e-01 -6.00825846e-01\n",
      "  -1.42674041e+00 -8.42585042e-02 -7.33945966e-01  5.08117527e-02\n",
      "  -5.53273499e-01 -8.57320547e-01 -1.37388384e+00  1.86585879e+00\n",
      "   1.63882697e+00  5.96732438e-01  1.14762664e+00 -3.85005802e-01\n",
      "   2.08175206e+00 -2.04218102e+00  2.76589513e-01 -2.03071809e+00\n",
      "  -1.72734812e-01 -3.58895719e-01]\n",
      " [-6.04833066e-01  1.94698501e+00  4.46396433e-02  1.79156578e+00\n",
      "  -5.00937700e-02  4.35284287e-01  1.83667481e-01  1.52397120e+00\n",
      "   1.62293899e+00 -3.70812893e-01 -2.46960416e-01 -9.71417844e-01\n",
      "  -1.34094846e+00  9.38521385e-01  2.05825716e-01 -1.12258351e+00\n",
      "   3.88505101e-01 -7.07417250e-01 -4.04397011e-01 -4.56317961e-02\n",
      "   1.08491147e+00 -1.25942242e+00  3.69427979e-01 -1.46712109e-01\n",
      "   4.84185338e-01 -1.57254905e-01  8.19753557e-02  1.54884592e-01\n",
      "  -5.78197539e-01  2.26558939e-01  1.01752184e-01  1.05692983e+00\n",
      "  -5.46343863e-01  1.49244428e+00 -1.28162515e+00  5.77251673e-01\n",
      "   3.88470560e-01 -1.25305736e+00 -1.25201714e+00  2.59141296e-01\n",
      "   1.38947642e+00 -1.82496399e-01  3.56961817e-01  1.33461848e-01\n",
      "  -6.64901018e-01  5.02582729e-01 -2.24354103e-01 -4.21293359e-03\n",
      "  -9.56807911e-01 -6.70450091e-01]\n",
      " [-7.11574972e-01  7.35360205e-01 -1.96901664e-01 -1.50632942e+00\n",
      "   2.39253566e-01  7.06367135e-01  7.69597709e-01  9.54931378e-01\n",
      "   1.11502178e-01 -2.24878415e-01  7.09988058e-01  3.48309278e-02\n",
      "   1.53426394e-01  1.46887913e-01 -1.00708842e+00  1.33420095e-01\n",
      "  -1.40467894e+00 -6.35135889e-01 -6.67340696e-01  5.05149007e-01\n",
      "   1.11494131e-01 -1.19730413e+00 -2.51091979e-02  1.60214806e+00\n",
      "   1.23983181e+00  2.84560394e+00 -5.14236331e-01 -6.42466903e-01\n",
      "   5.69374442e-01 -3.55174184e-01 -6.04669809e-01 -6.59350932e-01\n",
      "  -2.18486637e-01 -3.70521545e-01  2.27686214e+00  2.69236040e+00\n",
      "   6.54290318e-01 -2.69812375e-01 -5.39645791e-01 -4.91702199e-01\n",
      "   5.10181606e-01 -4.40897584e-01 -1.01839221e+00  4.82392877e-01\n",
      "   1.30852401e+00 -3.87795597e-01  1.10461235e+00  6.60968125e-02\n",
      "   2.06779823e-01 -9.62580740e-01]\n",
      " [ 1.06412935e+00  3.38216364e-01  4.79343444e-01 -9.50482011e-01\n",
      "  -3.73658001e-01  1.05980456e+00  9.43862975e-01  6.00994051e-01\n",
      "  -4.19341326e-02 -5.60994148e-01 -9.97854590e-01  6.11303151e-01\n",
      "   1.07164907e+00  1.54143178e+00  9.28862989e-01  3.88966560e-01\n",
      "  -6.17703915e-01 -1.88306439e+00 -4.99178708e-01  2.37899736e-01\n",
      "   4.10072953e-01 -3.62269491e-01 -1.31981826e+00 -3.86339217e-01\n",
      "   4.91848588e-01 -1.10272968e+00  1.27204522e-01 -3.74033712e-02\n",
      "   1.97999254e-01 -1.05067670e-01 -1.33483648e+00 -9.46699440e-01\n",
      "  -6.83698714e-01  1.89576700e-01 -6.31941974e-01 -4.23527300e-01\n",
      "   2.03629464e-01  6.47434115e-01 -2.47936106e+00 -2.09172440e+00\n",
      "   4.25275028e-01 -3.78558785e-01  1.46450412e+00  1.11579262e-01\n",
      "   2.63079643e-01  8.60633910e-01  1.32673669e+00 -8.40652525e-01\n",
      "   2.96222806e-01  1.29513001e+00]\n",
      " [-3.93040985e-01  4.56821024e-01  1.14732552e+00  1.43569136e+00\n",
      "   1.76242781e+00  1.25779033e+00 -4.66357350e-01 -5.03972828e-01\n",
      "   1.77820492e+00  1.71279564e-01 -6.86808407e-01  8.45005274e-01\n",
      "   7.00239241e-01  6.50711477e-01 -4.91629720e-01 -1.07302547e+00\n",
      "  -5.90270638e-01  1.42693853e+00  1.76554489e+00 -7.19082177e-01\n",
      "  -8.93170759e-02 -7.62637913e-01  8.13481629e-01 -5.67107797e-01\n",
      "  -1.42868087e-01  4.02879804e-01  1.59319150e+00 -1.79892933e+00\n",
      "  -3.49816322e-01 -1.31724507e-01 -1.57394278e+00  8.37452233e-01\n",
      "  -1.23157585e+00 -1.90845883e+00  1.18504381e+00  2.13676977e+00\n",
      "   1.00699484e+00 -1.33424163e+00  1.81500900e+00 -1.59391686e-01\n",
      "  -1.26808360e-01  1.27304637e+00  1.89877939e+00 -1.12839174e+00\n",
      "   1.52218008e+00 -6.30301654e-01 -8.79240811e-01 -1.99509943e+00\n",
      "   1.06929338e+00 -4.86776792e-02]\n",
      " [ 1.73909295e+00  2.76290476e-01  2.36694794e-03 -7.30554938e-01\n",
      "  -1.43919550e-02  3.89323831e-01  1.05326033e+00 -1.58698523e+00\n",
      "  -4.79120374e-01 -1.20665717e+00  5.66439629e-01 -1.15471888e+00\n",
      "  -8.45814943e-02  2.19045743e-01  6.04946256e-01 -5.21185994e-01\n",
      "   2.39143699e-01 -1.08506703e+00  4.78459567e-01  1.29047203e+00\n",
      "   5.76247752e-01 -7.56559297e-02 -1.44717932e+00 -1.96797892e-01\n",
      "  -1.37817681e-01 -3.21694702e-01 -8.30629766e-01  1.21680379e+00\n",
      "  -5.49737811e-01  2.23893121e-01 -2.23655373e-01 -2.00518942e+00\n",
      "  -1.04908693e+00  9.25722480e-01 -2.09514543e-01 -1.42484963e+00\n",
      "   1.63474202e+00 -3.88812833e-02 -1.04701507e+00  1.05407798e+00\n",
      "  -2.72513485e+00 -1.14489090e+00  1.22264302e+00  1.61319077e-01\n",
      "  -1.56233513e+00 -8.69335890e-01  7.09570467e-01  2.59840429e-01\n",
      "   8.90154004e-01  6.09794319e-01]\n",
      " [ 1.09124891e-01 -1.34165478e+00  2.09817457e+00 -3.63259256e-01\n",
      "   6.57690406e-01  9.38875914e-01 -3.56158108e-01  2.80534595e-01\n",
      "  -1.09162462e+00 -4.19730246e-02 -8.31216723e-02  6.44802034e-01\n",
      "   9.51171339e-01  1.30582318e-01 -1.47000504e+00 -3.90996814e-01\n",
      "  -1.59930098e+00  6.59196228e-02 -4.02132235e-02  1.79016382e-01\n",
      "   3.96214515e-01  1.43365574e+00  5.74232936e-01  5.56924231e-02\n",
      "  -9.45752680e-01 -1.84759033e+00 -1.54369265e-01 -8.64123046e-01\n",
      "  -3.51423204e-01 -7.25056946e-01 -5.51612318e-01 -7.50139356e-01\n",
      "  -1.52749038e+00 -1.38117179e-01  9.63248551e-01 -1.16410184e+00\n",
      "  -8.62740517e-01 -1.95213008e+00  1.18691123e+00  2.55420804e-01\n",
      "  -4.37133312e-01 -1.69883430e-01 -1.22461092e+00  2.23293996e+00\n",
      "  -5.72147131e-01  2.93174267e-01  1.01057792e+00 -9.84423086e-02\n",
      "   6.91966355e-01  2.25132033e-01]\n",
      " [ 5.47542036e-01 -8.92400563e-01 -1.36501718e+00  6.91033661e-01\n",
      "  -4.32110518e-01 -1.88729495e-01  2.85891294e-01 -9.14964795e-01\n",
      "   1.69513321e+00  5.72629869e-01  6.93743765e-01 -7.35232830e-01\n",
      "   2.27541089e+00  1.55443645e+00  1.34282187e-01 -1.24514484e+00\n",
      "  -4.72183496e-01 -3.45487952e-01  7.95483813e-02  5.96302986e-01\n",
      "  -8.43189418e-01 -2.56887484e+00  4.26913202e-01  2.63596550e-02\n",
      "   6.49290621e-01  1.16462660e+00  4.25051212e+00 -2.11378187e-03\n",
      "  -7.97760189e-01 -2.94192344e-01 -2.39079013e-01 -4.07025695e-01\n",
      "  -7.60577977e-01 -2.16555190e+00 -9.13379252e-01  9.01426315e-01\n",
      "   1.05937576e+00  2.13085741e-01  1.37732387e-01  1.86818504e+00\n",
      "   1.33052260e-01 -1.36067510e+00  1.79154706e+00  1.08167708e+00\n",
      "  -2.03371242e-01  5.70989132e-01  4.28786665e-01  3.69993560e-02\n",
      "   6.78585708e-01  5.96967220e-01]\n",
      " [-1.02040029e+00 -1.49791324e+00  3.52454722e-01  1.49715340e+00\n",
      "  -2.14659169e-01 -1.31526038e-01  2.00988889e+00 -1.02505639e-01\n",
      "  -6.98818117e-02 -1.47190675e-01  1.59034264e+00  1.01617122e+00\n",
      "   5.09223104e-01  1.23037457e-01  4.46996748e-01 -3.07386339e-01\n",
      "  -5.84380627e-01 -2.21527666e-01 -6.52679324e-01  4.95797813e-01\n",
      "  -1.40755916e+00 -4.58328575e-02  1.35720038e+00 -7.23016798e-01\n",
      "   8.88757765e-01 -2.43372515e-01 -5.56008160e-01 -2.88640261e-01\n",
      "  -1.42991384e-02 -8.22689712e-01 -1.45548177e+00 -5.56457162e-01\n",
      "   1.75807893e+00 -2.58210182e+00  2.92943060e-01 -2.94513605e-03\n",
      "  -8.03346574e-01  1.60354471e+00  1.67494431e-01 -4.48848635e-01\n",
      "   1.00593066e+00 -4.11439776e-01  2.04524612e+00  5.18449306e-01\n",
      "  -4.79614511e-02 -9.12849009e-01  8.86194766e-01 -7.39747345e-01\n",
      "   1.14483213e+00  3.09239123e-02]\n",
      " [ 5.38744152e-01  8.75363946e-02  3.68777603e-01 -9.38905895e-01\n",
      "   8.32450211e-01  4.01388019e-01  1.13481987e+00 -4.92528796e-01\n",
      "  -1.81979823e+00  5.46733141e-01 -2.25381210e-01 -1.55754954e-01\n",
      "   8.04177999e-01  2.26288307e-02  1.64203420e-01  5.35017967e-01\n",
      "   4.61674422e-01  6.50181353e-01 -1.86644900e+00 -6.55589163e-01\n",
      "  -1.66351748e+00  2.58280218e-01 -5.94117701e-01  3.01713347e-01\n",
      "   3.48022372e-01  2.52892494e-01 -6.97762609e-01  2.33929381e-01\n",
      "  -1.05003417e+00  1.45409346e+00 -3.88470083e-01 -2.00067520e+00\n",
      "  -8.20480585e-01  3.41754019e-01 -9.37788308e-01 -1.69338322e+00\n",
      "   6.50739193e-01 -1.14641559e+00  2.83726633e-01 -5.30157030e-01\n",
      "   1.54444528e+00  7.58368909e-01  6.03750467e-01 -5.49155176e-01\n",
      "  -3.38872582e-01 -7.66578257e-01 -1.27057099e+00  6.71743631e-01\n",
      "  -1.17451155e+00 -7.15418816e-01]]\n",
      "<NDArray 14x50 @cpu(0)>\n",
      "epoch 1,loss 96073.125000\n",
      "epoch 2,loss 63311.644531\n",
      "epoch 3,loss 35963.152344\n",
      "epoch 4,loss 19712.310547\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 5,loss 11436.534180\n",
      "epoch 6,loss 8220.606445\n",
      "epoch 7,loss 6499.437500\n",
      "epoch 8,loss 5390.225586\n",
      "epoch 9,loss 4485.451660\n",
      "epoch 10,loss 3757.813477\n",
      "epoch 11,loss 3165.031250\n",
      "epoch 12,loss 2681.996338\n",
      "epoch 13,loss 2270.509033\n",
      "epoch 14,loss 1917.114868\n",
      "epoch 15,loss 1639.478394\n",
      "epoch 16,loss 1399.057983\n",
      "epoch 17,loss 1206.057007\n",
      "epoch 18,loss 983.661621\n",
      "epoch 19,loss 849.136047\n",
      "epoch 20,loss 691.215271\n",
      "epoch 21,loss 577.435608\n",
      "epoch 22,loss 472.924591\n",
      "epoch 23,loss 409.931793\n",
      "epoch 24,loss 319.798828\n",
      "epoch 25,loss 272.440765\n",
      "epoch 26,loss 225.501663\n",
      "epoch 27,loss 185.810120\n",
      "epoch 28,loss 154.775192\n",
      "epoch 29,loss 134.998947\n",
      "epoch 30,loss 107.465118\n",
      "epoch 31,loss 88.490471\n",
      "epoch 32,loss 72.499275\n",
      "epoch 33,loss 60.326050\n",
      "epoch 34,loss 50.330128\n",
      "epoch 35,loss 41.145870\n",
      "epoch 36,loss 34.174843\n",
      "epoch 37,loss 28.298517\n",
      "epoch 38,loss 23.723448\n",
      "epoch 39,loss 19.588995\n",
      "epoch 40,loss 16.113024\n",
      "epoch 41,loss 13.333144\n",
      "epoch 42,loss 11.047302\n",
      "epoch 43,loss 9.430569\n",
      "epoch 44,loss 7.687452\n",
      "epoch 45,loss 6.376023\n",
      "epoch 46,loss 5.277668\n",
      "epoch 47,loss 4.457007\n",
      "epoch 48,loss 3.800375\n",
      "epoch 49,loss 3.067090\n",
      "epoch 50,loss 2.564919\n",
      "epoch 51,loss 2.185989\n",
      "epoch 52,loss 1.848948\n",
      "epoch 53,loss 1.486207\n",
      "epoch 54,loss 1.255347\n",
      "epoch 55,loss 1.056474\n",
      "epoch 56,loss 0.943753\n",
      "epoch 57,loss 0.762344\n",
      "epoch 58,loss 0.617528\n",
      "epoch 59,loss 0.529215\n",
      "epoch 60,loss 0.448835\n",
      "epoch 61,loss 0.374569\n",
      "epoch 62,loss 0.353611\n",
      "epoch 63,loss 0.279322\n",
      "epoch 64,loss 0.228501\n",
      "epoch 65,loss 0.192771\n",
      "epoch 66,loss 0.168119\n",
      "epoch 67,loss 0.145480\n",
      "epoch 68,loss 0.118079\n",
      "epoch 69,loss 0.100364\n",
      "epoch 70,loss 0.085894\n",
      "epoch 71,loss 0.073059\n",
      "epoch 72,loss 0.062044\n",
      "epoch 73,loss 0.052493\n",
      "epoch 74,loss 0.046674\n",
      "epoch 75,loss 0.037857\n",
      "epoch 76,loss 0.032979\n",
      "epoch 77,loss 0.027791\n",
      "epoch 78,loss 0.023268\n",
      "epoch 79,loss 0.019926\n",
      "epoch 80,loss 0.017155\n",
      "epoch 81,loss 0.014463\n",
      "epoch 82,loss 0.012388\n",
      "epoch 83,loss 0.011187\n",
      "epoch 84,loss 0.009224\n",
      "epoch 85,loss 0.007823\n",
      "epoch 86,loss 0.006868\n",
      "epoch 87,loss 0.005845\n",
      "epoch 88,loss 0.004839\n",
      "epoch 89,loss 0.004760\n",
      "epoch 90,loss 0.003561\n",
      "epoch 91,loss 0.003056\n",
      "epoch 92,loss 0.002622\n",
      "epoch 93,loss 0.002267\n",
      "epoch 94,loss 0.001934\n",
      "epoch 95,loss 0.001790\n",
      "epoch 96,loss 0.001436\n",
      "epoch 97,loss 0.001252\n",
      "epoch 98,loss 0.001069\n",
      "epoch 99,loss 0.000931\n",
      "epoch 100,loss 0.000782\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import d2lzh as d2l\n",
    "import xlrd\n",
    "import random\n",
    "import math\n",
    "from IPython import display\n",
    "from matplotlib import pyplot as plt\n",
    "from mxnet import autograd, nd\n",
    "batch_size =2\n",
    "num_inputs = 14\n",
    "num_outputs = 1\n",
    "num_hiddens=50\n",
    "num_hiddens1=30\n",
    "\n",
    "\n",
    "w = nd.random.normal(scale=1, shape=(num_inputs, num_hiddens))\n",
    "b = nd.zeros(num_hiddens)\n",
    "w1=nd.random.normal(scale=1, shape=(num_hiddens, num_hiddens1))\n",
    "b1= nd.zeros(num_hiddens1)\n",
    "w2=nd.random.normal(scale=1, shape=(num_hiddens1, num_outputs))\n",
    "b2= nd.zeros(num_outputs)\n",
    "\n",
    "w.attach_grad()\n",
    "b.attach_grad()\n",
    "w1.attach_grad()\n",
    "b1.attach_grad()\n",
    "w2.attach_grad()\n",
    "b2.attach_grad()\n",
    "\n",
    "params=[w,b,w1,b1,w2,b2]\n",
    "print(w)\n",
    "def use_svg_display():\n",
    "    # 用矢量图显示\n",
    "    display.set_matplotlib_formats('svg')\n",
    "\n",
    "def set_figsize(figsize=(3.5, 2.5)):\n",
    "    use_svg_display()\n",
    "    # 设置图的尺寸\n",
    "    plt.rcParams['figure.figsize'] = figsize\n",
    "\n",
    "def squared_loss(y_hat, y):\n",
    "    return (y_hat - y) ** 2 / batch_size\n",
    "\n",
    "def relu(X):\n",
    "    return nd.maximum(X,0)\n",
    "\n",
    "def net(X):\n",
    "    H=relu(nd.dot(X,w)+b)\n",
    "    Y=nd.dot(H, w1) + b1\n",
    "    return nd.dot(Y, w2) + b2\n",
    "\n",
    "def excel2matrix(path):\n",
    "    data = xlrd.open_workbook(path)\n",
    "    table = data.sheets()[0]\n",
    "    nrows = table.nrows  # 行数\n",
    "    ncols = table.ncols  # 列数\n",
    "    datamatrix = nd.random.normal(scale=1,shape=(nrows, ncols))\n",
    "    for i in range(nrows):\n",
    "        rows = table.row_values(i)\n",
    "        datamatrix[i,:] = rows\n",
    "    return datamatrix\n",
    " \n",
    "def data_iter(batch_size, features, labels):\n",
    "    num_examples = len(features)\n",
    "    indices = list(range(num_examples))\n",
    "    random.shuffle(indices)  # 样本的读取顺序是随机的\n",
    "    for i in range(0, num_examples, batch_size):\n",
    "        j = nd.array(indices[i: min(i + batch_size, num_examples)])\n",
    "        yield features.take(j), labels.take(j)  # take函数根据索引返回对应元素\n",
    "# def cross_entropy(y_hat, y):\n",
    "#     return -nd.pick(y_hat, y).log()\n",
    "# def accuracy(y_hat, y):\n",
    "#     return (y_hat.argmax(axis=1) == y.astype('float32')).mean().asscalar()\n",
    "\n",
    "# def evaluate_accuracy(data_iter, net):\n",
    "#     acc_sum, n = 0.0, 0\n",
    "#     for X, y in data_iter:\n",
    "#         y = y.astype('float32')\n",
    "#         acc_sum += (net(X).argmax(axis=1) == y).sum().asscalar()\n",
    "#         n += y.size\n",
    "#     return acc_sum / n\n",
    "\n",
    "num_epochs, lr = 100, 0.00001\n",
    "\n",
    "def sgd(params, lr, batch_size):  \n",
    "    for param in params:\n",
    "        param[:] = param - lr * param.grad / 2\n",
    "\n",
    "def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,\n",
    "              params=None, lr=None):\n",
    "    for epoch in range(num_epochs):\n",
    "        for X, y in data_iter(batch_size,x,x_label):\n",
    "            with autograd.record():\n",
    "                y_hat = net(X)\n",
    "#                 print('X')\n",
    "#                 print(X)\n",
    "#                 print('y_hat')\n",
    "#                 print(y_hat)\n",
    "#                 print('y')\n",
    "#                 print(y)\n",
    "#                 print('[W,b]')\n",
    "#                 print([w,b])\n",
    "                l = loss(y_hat, y)\n",
    "            l.backward()   #求梯度\n",
    "            sgd(params, lr, batch_size)    #更新wb权重   \n",
    "#         print(params)\n",
    "        train_l_sum =loss(net(x),x_label)  #误差\n",
    "        print('epoch %d,loss %f' % (epoch + 1,train_l_sum.mean().asnumpy()))\n",
    "\n",
    "\n",
    "pathX = 'DD2_train.xls'  #  113.xlsx 在当前文件夹下\n",
    "pathX2 = 'DD2_train_label.xls'  #  113.xlsx 在当前文件夹下\n",
    "pathX3 = 'DD2_test.xls'  #  113.xlsx 在当前文件夹下\n",
    "x = excel2matrix(pathX)\n",
    "x_label=excel2matrix(pathX2)\n",
    "y_test=excel2matrix(pathX3)\n",
    "y_label=nd.zeros((y_test.shape[0],1))\n",
    "\n",
    "train_iter=data_iter(batch_size,x,x_label)\n",
    "test_iter=data_iter(batch_size,y_test,y_label)\n",
    "train_ch3(net, train_iter, test_iter, squared_loss, num_epochs, batch_size,params, lr)\n",
    "#set_figsize()\n",
    "#plt.scatter(x[:, 1].asnumpy(), x_label[:, 0].asnumpy(), 1);  # 加分号只显示图\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e86025e9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[[-7.4420196e-01 -1.2479534e-02  5.5598861e-01  9.0991426e-01\n",
      "   1.3695257e+00  1.2692747e+00  1.1866250e+00 -1.5450190e+00\n",
      "  -7.1123868e-01  7.3350334e-01  3.6060417e-01  2.1503046e-01\n",
      "   1.5925191e-01  1.3311639e-01]\n",
      " [-8.5154027e-01 -5.0271142e-01 -1.2340350e+00 -3.1531683e-01\n",
      "   1.2344276e+00 -1.2594148e+00  7.9946441e-04 -1.5006772e+00\n",
      "   7.0311075e-01 -7.7648264e-01  1.1197467e-01  1.0635081e-01\n",
      "  -1.9643685e-01 -1.8263368e-01]\n",
      " [ 5.9921944e-01 -7.9685056e-01 -2.0267024e+00 -1.6347965e+00\n",
      "   3.6784410e-01  8.8987130e-01 -6.9464111e-01 -1.2366467e+00\n",
      "  -8.2228355e-02 -7.3929048e-01 -6.9607115e-01  1.4415580e+00\n",
      "   6.6373712e-01  5.3909183e-01]\n",
      " [-8.5288352e-01 -1.0267525e+00 -1.0120882e+00 -9.0090521e-03\n",
      "  -1.1566463e+00 -5.2169997e-01 -1.2147511e+00  3.2968190e-01\n",
      "   1.6080123e+00 -1.3996485e+00 -1.6773349e-01  1.3328784e+00\n",
      "   1.6650331e-01  4.8241161e-02]\n",
      " [-1.4173681e+00 -1.6462047e-01  5.9572232e-01  2.2661231e-01\n",
      "   7.2940940e-01  5.7928419e-01  5.0294727e-01  9.1670221e-01\n",
      "   1.1520361e+00  9.4424045e-01  1.5105156e+00  7.1185178e-01\n",
      "   1.8873491e+00  1.7444842e-01]\n",
      " [-7.0516235e-01 -1.4771593e-01 -6.6652530e-01 -1.8939800e+00\n",
      "   1.1124837e+00  1.3383948e+00 -3.9436972e-01  1.0822188e+00\n",
      "   1.3524507e-01  8.9240444e-01  1.5859270e-01  4.7896677e-01\n",
      "  -5.9276807e-01 -1.1385740e+00]\n",
      " [-7.1981114e-01 -1.0267525e+00 -8.3509254e-01 -1.7761693e+00\n",
      "  -4.7314182e-01 -8.1627095e-01 -2.2269526e-01 -1.5984339e+00\n",
      "  -1.9555763e+00 -1.6081245e+00  1.4794369e+00  8.3605707e-01\n",
      "   1.7951605e+00  7.7355403e-01]\n",
      " [-6.1915898e-01 -1.0267525e+00 -9.7155172e-01 -1.6583586e+00\n",
      "  -1.4231095e+00  2.1642057e-02 -2.7674094e-01  3.5074857e-01\n",
      "  -1.2099332e+00 -5.0527185e-01  1.0754139e+00 -1.4617414e+00\n",
      "   1.8973584e+00  1.0491009e+00]\n",
      " [-8.5838354e-01  4.9465692e-01  1.4907341e+00  4.6223369e-01\n",
      "  -8.1177598e-01 -1.2297276e+00 -4.7782260e-01  1.2306436e+00\n",
      "   7.4531966e-01  4.4674417e-01 -1.9392185e+00 -1.6014724e+00\n",
      "  -1.8147773e+00  1.1054627e+00]\n",
      " [ 3.4716700e-03  9.9090487e-02 -9.0587191e-02  2.2661231e-01\n",
      "   7.2763181e-01  7.5601810e-01  8.1927341e-01  1.0162879e+00\n",
      "  -8.7441689e-01  2.6317522e-01  2.3628941e-01  7.5299479e-02\n",
      "   1.7032771e-01 -5.3946835e-01]\n",
      " [ 1.9746743e-01 -1.0267525e+00 -7.5281566e-01 -2.4463043e-01\n",
      "  -4.3706366e-01  5.2831266e-02 -1.0508657e+00 -1.7175863e+00\n",
      "  -7.6764339e-01 -6.6279054e-01  1.4794369e+00  1.5502377e+00\n",
      "   7.1515411e-01  1.2766358e+00]\n",
      " [ 2.3878084e-01 -1.0098479e+00 -5.9026867e-01 -3.2571189e-02\n",
      "   1.3302220e-01 -2.3993476e-01 -4.9133402e-01 -1.6560874e+00\n",
      "  -4.1335392e-01 -6.2417889e-01 -3.8528427e-01  2.4608180e-01\n",
      "   8.3870572e-01  1.0282261e+00]\n",
      " [-1.1178368e+00  1.3398843e+00  8.5459346e-01  4.6223369e-01\n",
      "   1.0396018e+00  8.4021407e-01  1.0028697e+00  4.0604824e-01\n",
      "   1.6220911e+00  7.1680599e-01 -7.4497439e-02 -9.9597144e-01\n",
      "  -3.0903670e-01  4.4373283e-01]\n",
      " [-1.2880278e+00  1.1708388e+00  9.7098511e-01  9.3347639e-01\n",
      "   2.1932688e+00  2.0187020e+00  1.5741501e-02  9.0861702e-01\n",
      "  -4.1720274e-01  1.6497833e+00 -1.6284317e+00 -1.4617414e+00\n",
      "  -1.7092741e+00  5.6480640e-01]\n",
      " [-1.2738094e+00  6.6370237e-01  1.1355388e+00  1.1690978e+00\n",
      "   1.3240190e+00  1.0640094e+00  4.9122646e-02  1.0822188e+00\n",
      "   1.3524507e-01  1.3511770e+00 -1.2554874e+00 -7.9413778e-01\n",
      "  -1.7548125e+00 -8.1029904e-01]]\n",
      "<NDArray 15x14 @cpu(0)>\n"
     ]
    }
   ],
   "source": [
    "y_test=excel2matrix(pathX3)\n",
    "a=net(y_test)\n",
    "print(y_test)\n",
    "#set_figsize()\n",
    "#plt.scatter(x[:, 1].asnumpy(), x_label[:, 0].asnumpy(), 1);  # 加分号只显示图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77210598",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 800.00995]\n",
      " [ 509.95886]\n",
      " [ 335.9656 ]\n",
      " [ 200.00958]\n",
      " [ 710.00165]\n",
      " [ 720.02386]\n",
      " [ 200.0004 ]\n",
      " [ 199.99348]\n",
      " [1099.9987 ]\n",
      " [ 865.95703]\n",
      " [ 199.95312]\n",
      " [ 210.1051 ]\n",
      " [ 869.6034 ]\n",
      " [1921.5442 ]\n",
      " [1566.9033 ]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a1=a.asnumpy()\n",
    "print(a1)\n",
    "np.savetxt(\"./result.txt\",a1,fmt='%f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c373e645",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:gluon] *",
   "language": "python",
   "name": "conda-env-gluon-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
